package adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.squareup.picasso.Picasso;
import com.tikeyc.mobileplayer.R;

import org.xutils.common.Callback;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;

import domain.MediaItem;
import utils.TimeUtils;

/**
 * Created by public1 on 2017/1/5.
 */

public class VideoNetPagerAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<MediaItem> mediaNetItems;

    class ViewHolder {
        ImageView imageView_coverImg;
        TextView textView_movieName;
        TextView textView_summary;
    }

    public VideoNetPagerAdapter(Context context, ArrayList<MediaItem> mediaNetItems) {
        this.context = context;
        this.mediaNetItems = mediaNetItems;
    }

    @Override
    public int getCount() {
        return mediaNetItems.size();
    }

    @Override
    public Object getItem(int i) {
        return mediaNetItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {


        ViewHolder viewHolder;
        if (view == null) {
            view = View.inflate(context, R.layout.item_video_net_pager,null);
            //
            ImageView imageView_coverImg = (ImageView) view.findViewById(R.id.imageView_coverImg);
            TextView textView_movieName = (TextView) view.findViewById(R.id.textView_movieName);
            TextView textView_summary = (TextView) view.findViewById(R.id.textView_summary);
            //
            viewHolder = new ViewHolder();
            viewHolder.imageView_coverImg = imageView_coverImg;
            viewHolder.textView_movieName = textView_movieName;
            viewHolder.textView_summary = textView_summary;
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }


        MediaItem mediaNetItem = mediaNetItems.get(i);
        //1. xutils加载图片
//        x.image().loadDrawable(mediaNetItem.getCoverImg(), ImageOptions.DEFAULT, new Callback.CommonCallback<Drawable>() {
//            @Override
//            public void onSuccess(Drawable result) {
//
//            }
//
//            @Override
//            public void onError(Throwable ex, boolean isOnCallback) {
//
//            }
//
//            @Override
//            public void onCancelled(CancelledException cex) {
//
//            }
//
//            @Override
//            public void onFinished() {
//
//            }
//        });
        /**
         * Glide和Picasso都是非常完美的库。Glide加载图像以及磁盘缓存的方式都要优于Picasso，速度更快，
         * 并且Glide更有利于减少OutOfMemoryError的发生，GIF动画是Glide的杀手锏。不过Picasso的图片质量更高。你更喜欢哪个呢？
         * 虽然我使用了很长时间的Picasso，但是我得承认现在我更喜欢Glide。我的建议是使用Glide，
         * 但是将Bitmap格式换成 ARGB_8888、让Glide缓存同时缓存全尺寸和改变尺寸两种。
         * */
        //2. Picasso 加载图片
//        Picasso.with(context).load(mediaNetItem.getCoverImg()).placeholder(null).into(viewHolder.imageView_coverImg);
        //3. Glide 加载图片
        Glide.with(context)
                .load(mediaNetItem.getCoverImg())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(null)
                .error(null)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String s, Target<GlideDrawable> target, boolean b) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable glideDrawable, String s, Target<GlideDrawable> target, boolean b, boolean b1) {
                        return false;
                    }
                })
                .into(viewHolder.imageView_coverImg);
        //
        viewHolder.textView_movieName.setText(mediaNetItem.getMovieName());
        //
        viewHolder.textView_summary.setText(mediaNetItem.getSummary());

        return view;
    }
}
